<template>
  <div class="modefyName-container">
    <van-nav-bar
      title="编辑昵称"
      left-text="取消"
      right-text="确定"
      left-arrow
      @click-left="$emit('close')"
      @click-right="modefyNameFn"
    />

    <div class="field">
      <van-field
        v-model.trim="message"
        rows="2"
        autosize
        maxlength="15"
        type="textarea"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { modefyUserProfile } from "@/api/login";

export default {
  props: ["value"],
  data() {
    return {
      message: this.value,
    };
  },
  methods: {
    async modefyNameFn() {
      if (!this.message) {
        this.$toast("昵称不可为空");
        return;
      }
      this.$toast.loading({
        message: "修改中...",
        forbidClick: true,
        duration: 0,
      });
      try {
        await modefyUserProfile({
          name: this.message,
        });
        this.$emit("input", this.message);
        this.message = "";
        this.$emit("close");
        this.$toast("修改成功");
      } catch (error) {
        console.log(error);
        this.$toast("修改失败");
      }
    },
  },
};
</script>

<style scoped lang="less">
.field {
  padding: 20px;
  .van-field {
    border: 1px solid #ccc;
    width: calc(100vw - 40px);
  }
}
</style>